<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * index.ctrl
 * 全部商品列表控制器
 */
defined('IN_IA') or exit('Access Denied');
define('timeout', 600);//默认绑定时间为10分钟
$ops = array('test','prompt','cabinet_admin','cabinet_admin_button','door_teacher','door_teacher_button','door_student','door_student_button','cover');
$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'cover';
//$op = $_GPC['op'];
global $_W;
$rolearray = array('admin','teacher','student');
$rolenamearray =array(
    'admin'=>'管理员',
    'teacher'=>'教师',
    'student'=>'课代表'
);
$coverinfo =array(
    'admin'=>array(
        'generate'=>'door_teacher',
        'unbinddown'=>'door_teacher',
        'unbindself'=>'cabinet_admin',
        'rolename'=>'管理员',
        'selfdevice'=>'柜子',
        'downrolename'=>'教师',
        'downdevice'=>'柜门'
    ),
    'teacher'=>array(
        'generate'=>'door_student',
        'unbinddown'=>'door_student',
        'unbindself'=>'door_teacher',
        'rolename'=>'教师',
        'selfdevice'=>'柜门',
        'downrolename'=>'课代表',
        'downdevice'=>'柜门'
    ),
    'student'=>array(
        'unbindself'=>'door_student',
        'rolename'=>'课代表',
        'selfdevice'=>'柜门',
    )    
);

if($op =='test'){
    global $_W;
    $openid = $_W['openid'];
    //$data = $this->post_open_door(36,1);
    print_r($openid);
    print_r('test function:to get openid');
}
if($op =='prompt'){
    $prompt = $_GPC['prompt'];
    include wl_template('tips/prompt');
}
if($op =='cabinet_admin'){
	$info_id = $_GPC['info_id'];
    $random = $_GPC['random'];
    $ifexist = pdo_get('homework_cabinet_admin', array('uniacid' => $_W['uniacid'] , 'device_info_id' => $info_id , 'random' => $random) ,array('time' ,'device_name' ,'door_num'));
    if($ifexist==null|$ifexist['time']==''|$ifexist['time']==0){
        $prompt = '绑定用二维码有错，请取得有效二维码！';
        include wl_template('tips/prompt');
        exit;
    }
    //print_r(gettype($ifexist['time']));字段类型为int，但取出是string
    $timecount = time() - intval($ifexist['time']);
    if($timecount>timeout){
        $prompt = '绑定用二维码时效已过，请取得有效二维码！';
        include wl_template('tips/prompt');
         exit;
    }
    $needsetup = false;
    if($ifexist['device_name']==''|$ifexist['door_num']=='0'){
        $needsetup = true;
    }else{
        $device_name = $ifexist['device_name'];
        $door_num = $ifexist['door_num'];
    }
    $already_name = false;
    $ifexistname = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$_W['openid']), array('name' ,'role'));
    if($ifexistname['role']!='' & $ifexistname['role']!='admin'){
        $prompt = '您的角色不是管理员，无法使用此功能！';
        include wl_template('tips/prompt');
        exit;        
    }    
    if($ifexistname['name']!=''){
        $already_name = true;
        $admin_name = $ifexistname['name'];
    }
	include wl_template('scan/scan_cabinet_admin');
    exit;
}
if($op =='cabinet_admin_button'){//按钮后只是返回提示信息
    $role = $rolearray[0];
    $info_id = $_POST['info_id'];
    $openid = $_POST['openid'];
    $device_name = $_POST['device_name'];
    $door_num = $_POST['door_num'];    
    $name = $_POST['name'];
    $data = '';
    
    //echo "<script>alert('提示内容')</script>";
    
    //判断用户表是否已有，如果没有，则插入再查找
    $userid = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$openid), array('id' ,'name' ,'role'));
    if($userid==null){
        $arr = array('uniacid'=>$_W['uniacid'] ,'openid'=>$openid ,'role'=>$role ,'name'=>$name);
        pdo_insert('homework_user' , $arr);  
        $userid = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id'));        
    }else{//不为空，说明已有该用户，先检查该用户是否为admin
        if($userid['role']!=$role){
            $data = '您的角色不是管理员，请通过后台修改角色';
            print_r($data);
            exit;
        }
        
    } 
     
    //判断柜子与管理员表的userid字段
    $ifempty = pdo_get('homework_cabinet_admin', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id), array('userid'));
    if($ifempty['userid'] != ''){
        $arr = explode(',',$ifempty['userid']);
        if(in_array($userid['id'],$arr)){
            $data = '您已经绑定过该柜子！';
            print_r($data);
            exit;
        } 
        $userid = $ifempty['userid'].','.$userid['id'];
    }      
    else
        $userid = $userid['id'];
    
    //此时userid为更新好的
    $arr = array('userid'=>$userid ,'device_name' => $device_name ,'door_num' => $door_num);//设备号和名称都可能重复更新，但这么写节省代码
    pdo_update('homework_cabinet_admin' , $arr ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id));
    
    $data = '您已经成功绑定！设备信息ID为：'.$info_id.'，设备名称为：'.$device_name;
    print_r($data);
}
if($op =='door_teacher'){
	$info_id = $_GPC['info_id'];
    $door = $_GPC['door']; 
    $random = $_GPC['random'];
    $device_name = pdo_get('homework_cabinet_admin', array('uniacid' => $_W['uniacid'] , 'device_info_id' => $info_id) ,array('device_name'));
	$device_name = $device_name['device_name'];
    $ifexist = pdo_get('homework_door_teacher', array('uniacid' => $_W['uniacid'] , 'device_info_id' => $info_id , 'door' => $door ,'random' => $random) ,array('time'));
    if($ifexist==null|$ifexist['time']==''|$ifexist['time']==0){
        $prompt = '绑定用二维码有错，请取得有效二维码！';
        include wl_template('tips/prompt');
        exit;
    }

    $timecount = time() - intval($ifexist['time']);
    if($timecount>timeout){
        $prompt = '绑定用二维码时效已过，请取得有效二维码！';
        include wl_template('tips/prompt');
         exit;
    }
    $already_name = false;
    $ifexistname = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$_W['openid']), array('name' ,'role'));
    if($ifexistname['role']!='' & $ifexistname['role']!='teacher'){
        $prompt = '您的角色不是教师，无法使用此功能！';
        include wl_template('tips/prompt');
        exit;        
    }
    if($ifexistname['name']!=''){//这里不检查角色，交给button
        $already_name = true;
        $teacher_name = $ifexistname['name'];
    }
	include wl_template('scan/scan_door_teacher');
    exit;
}
if($op =='door_teacher_button'){//按钮后只是返回提示信息
    $role = $rolearray[1];
    $device_name = $_POST['device_name'];
    $info_id = $_POST['info_id'];
    $door = $_POST['door'];
    
    $openid = $_POST['openid'];       
    $name = $_POST['name'];
    
    $data = '';
    
    //判断用户表是否已有，如果没有，则插入再查找
    $userid = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$openid), array('id' ,'name' ,'role'));
    if($userid==null){
        $arr = array('uniacid'=>$_W['uniacid'] ,'openid'=>$openid ,'role'=>$role ,'name'=>$name);
        pdo_insert('homework_user' , $arr);  
        $userid = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id'));        
    }else{//不为空，说明已有该用户，先检查该用户是否为admin
        if($userid['role']!=$role){
            $data = '您的角色不是教师，请通过后台修改角色';
            print_r($data);
            exit;
        }      
    }
    
    //判断柜门与教师表的userid字段
    $ifempty = pdo_get('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id ,'door'=>$door), array('userid'));
    if($ifempty['userid'] != ''){
        $data = '该柜门已经被其他教师绑定！';
        print_r($data);
        exit;
    }      
    else
        $userid = $userid['id'];
    
    //此时userid为更新好的
    $arr = array('userid'=>$userid);
    pdo_update('homework_door_teacher' , $arr ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id,'door'=>$door));
    
    $data = '您已经成功绑定！设备信息ID为：'.$info_id.'，设备名称为：'.$device_name.'，柜门号为：'.$door;
    print_r($data);
}
if($op =='door_student'){
	$info_id = $_GPC['info_id'];
    $door = $_GPC['door']; 
    $random = $_GPC['random'];
    $device_name = pdo_get('homework_cabinet_admin', array('uniacid' => $_W['uniacid'] , 'device_info_id' => $info_id) ,array('device_name'));
	$device_name = $device_name['device_name']; 
    $ifexist = pdo_get('homework_door_student', array('uniacid' => $_W['uniacid'] , 'device_info_id' => $info_id , 'door' => $door ,'random' => $random) ,array('time'));
    if($ifexist==null|$ifexist['time']==''|$ifexist['time']==0){
        $prompt = '绑定用二维码有错，请取得有效二维码！';
        include wl_template('tips/prompt');
        exit;
    }

    $timecount = time() - intval($ifexist['time']);
    if($timecount>timeout){
        $prompt = '绑定用二维码时效已过，请取得有效二维码！';
        include wl_template('tips/prompt');
        exit;
    }
    $already_name = false;
    $ifexistname = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$_W['openid']), array('name' ,'role'));
    if($ifexistname['role']!='' & $ifexistname['role']!='student'){
        $prompt = '您的角色不是课代表，无法使用此功能！';
        include wl_template('tips/prompt');
        exit;        
    }    
    if($ifexistname['name']!=''){//上面检查角色，交给button也检查
        $already_name = true;
        $student_name = $ifexistname['name'];
    }
	include wl_template('scan/scan_door_student');
    exit;
}
if($op =='door_student_button'){//按钮后只是返回提示信息
    $role = $rolearray[2];
    $device_name = $_POST['device_name'];
    $info_id = $_POST['info_id'];
    $door = $_POST['door'];
    
    $openid = $_POST['openid'];       
    $name = $_POST['name'];
    $data = '';
   
    //判断用户表是否已有，如果没有，则插入再查找
    $userid = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'openid'=>$openid), array('id' ,'name' ,'role'));
    if($userid==null){
        $arr = array('uniacid'=>$_W['uniacid'] ,'openid'=>$openid ,'role'=>$role ,'name'=>$name);
        pdo_insert('homework_user' , $arr);  
        $userid = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id'));        
    }else{//不为空，说明已有该用户，先检查该用户是否为student
        if($userid['role']!=$role){
            $data = '您的角色不是课代表，请通过后台修改角色';
            print_r($data);
            exit;
        }
        
    } 
     
    //判断柜门与课代表的userid字段
    $ifempty = pdo_get('homework_door_student', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id ,'door'=>$door), array('userid'));
    if($ifempty['userid'] != ''){
        $arr = explode(',',$ifempty['userid']);
        if(in_array($userid['id'],$arr)){
            $data = '您已经绑定过该柜子！';
            print_r($data);
            exit;
        } 
        $userid = $ifempty['userid'].','.$userid['id'];
    }      
    else
        $userid = $userid['id'];
    
    //此时userid为更新好的，此时表里除了userid字段，其他都有了
    $arr = array('userid'=>$userid);
    pdo_update('homework_door_student' , $arr ,array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id,'door' => $door));
    
    $data = '您已经成功绑定！设备信息ID为：'.$info_id.'，设备名称为：'.$device_name.'，柜门号为：'.$door;
    print_r($data);
}
if($op =='cover'){
    $openid = $_W['openid'];

    $ifexist = pdo_get('homework_user', array('openid'=>$openid), array('id','role'));
    if($ifexist==null){
        $prompt = '请先使用作业柜的绑定功能';
        include wl_template('tips/prompt');
        exit;
    }
     $userid = $ifexist['id'];
     $role = $ifexist['role'];
     
     include wl_template('cover/cover');   
}
